package com.dcits.account.service.impl;

import com.dcits.account.constants.BaseContants;
import com.dcits.account.entity.cto.InputObject;
import com.dcits.account.entity.cto.OutputObject;
import com.dcits.account.entity.po.UserEntity;
import com.dcits.account.entity.po.UserEntityExample;
import com.dcits.account.mapper.UserEntityMapper;
import com.dcits.account.service.interfaces.IAccountService;
import com.dcits.account.service.interfaces.IUserService;
import com.dcits.account.util.ToolUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @Auther: 2843896844@qq.com
 * @Date: 2021/06/11/19:49
 * @Description:
 */
@Service
public class UserServiceImpl implements IUserService {

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    private UserEntityMapper userEntityMapper;

    @Autowired
    private IAccountService iAccountService;

    @Override
    public void loginVerifyUserphone(InputObject inputObject, OutputObject outputObject) {
        Map<String, Object> params = inputObject.getParams();
        String userphone = MapUtils.getString(params, "userPhone");
        if (StringUtils.isBlank(userphone)) {
            outputObject.setRtnCode(OutputObject.FAILED_CODE);
            outputObject.setRtnMsg("手机号不得为空");
            return;
        }
        UserEntityExample userEntityExample = new UserEntityExample();
        userEntityExample.createCriteria().andPhoneEqualTo(userphone);
        List<UserEntity> userEntities = userEntityMapper.selectByExample(userEntityExample);
        if (CollectionUtils.isEmpty(userEntities)) {
            outputObject.setRtnCode(OutputObject.FAILED_CODE);
            outputObject.setRtnMsg("该用户不存在");
            return;
        }
        if (CollectionUtils.isNotEmpty(userEntities) && userEntities.size() != 1) {
            outputObject.setRtnCode(OutputObject.FAILED_CODE);
            outputObject.setRtnMsg("系统内部错误，手机号应该是唯一的");
            return;
        }
        if (CollectionUtils.isNotEmpty(userEntities) && userEntities.size() == 1) {
            outputObject.setRtnCode(OutputObject.SUCCESS_CODE);
            outputObject.setRtnMsg("success");
            outputObject.getBean().put("user", userEntities.get(0));
            return;
        }
    }

    @Override
    public void verifyLogin(InputObject inputObject, OutputObject outputObject) {
        this.loginVerifyUserphone(inputObject, outputObject);
        if (outputObject.isFailed()) {
            return;
        }
        Map<String, Object> params = inputObject.getParams();
        String password = MapUtils.getString(params, "password");
        UserEntity user = (UserEntity) outputObject.getBean().get("user");
        String userPassword = user.getPassword();
        boolean equals = StringUtils.equals(password, userPassword);
        if (!equals) {
            outputObject.setRtnCode(OutputObject.FAILED_CODE);
            outputObject.setRtnMsg("密码错误");
            outputObject.getBean().remove("user");
        }
    }

    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public void register(InputObject inputObject, OutputObject outputObject) {
        Map<String, Object> params = inputObject.getParams();
        String username = MapUtils.getString(params, "username");
        String realName = MapUtils.getString(params, "realName");
        String userPhone = MapUtils.getString(params, "userPhone");
        String email = MapUtils.getString(params, "email");
        String password = MapUtils.getString(params, "password");
        Boolean allNotBlank = ToolUtil.allNotBlank(username, realName, userPhone, email, password);
        if (!allNotBlank) {
            outputObject.setRtnCode(OutputObject.FAILED_CODE);
            outputObject.setRtnMsg("请检查参数");
            return;
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        userEntity.setRealname(realName);
        userEntity.setPhone(userPhone);
        userEntity.setEmail(email);
        userEntity.setPassword(password);
        userEntity.setStatus(BaseContants.USER_STATUS_NORMAL);
        userEntity.setReputation(BaseContants.USER_REPUTATION_EXCELLENT);
        int insert = userEntityMapper.insertSelective(userEntity);
        if (insert != 1) {
            outputObject.setRtnCode(OutputObject.FAILED_CODE);
            outputObject.setRtnMsg("未知的原因新用户保存失败");
            return;
        }
        Integer userEntityId = userEntity.getId();
        inputObject.getParams().put("id", userEntityId);
        iAccountService.initNewAccount(inputObject, outputObject);
        if(!outputObject.isFailed()){
            outputObject.setRtnMsg("注册成功");
        }
    }
}
